<template>
	<view class="container">
		<scroll-view class="scroll" :scroll-y="true">
			<view class="scrollList" style="" v-for="(item,index) in inventypeListData" :key="index">
				<view class="scrollItem">
					<view class="box_content_oput_input">
						<checkbox-group style="transform: scale(0.8);" @change="checkboxChange($event,item)">
							<checkbox :value="item.Id" :checked="item.isChecked" />
						</checkbox-group>
					</view>
					<view class="">
						{{item.industryName}}
					</view>
				</view>
			</view>

		</scroll-view>
		
		<view class="fixView"> </view>
		<view class="fixBtn">
			<button @click="submitButton" type="primary">完成</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

				// 行业列表数据
				inventypeListData: [],
				// 公司信息
				CommpayInfo: null,
				// 存储选好的数组
				selectInvetTypeListData: []

			}
		},
		onShow() {
			this.CommpayInfo = uni.getStorageSync('CompanyInfo')
			// 获取行业列表数据
			this.getInventypeListData()
		},
		methods: {
			// 获取行业列表数据
			getInventypeListData() {
				this.$http("post", "/BO/InDustry/GetDataList", {
						CompanyId: this.CommpayInfo.Id
					})
					.then((res) => {
						if (res.Success) {
							res.Data.forEach(item => {
								this.$set(item, 'isChecked', false)
							})
							this.inventypeListData = res.Data
							console.log('获取行业列表数据', this.inventypeListData)
						}
					});
			},
			checkboxClick(item, index) {
				console.log('点击的是', item, index)
			},
			// 选择事件
			checkboxChange(event, obj) {
				this.inventypeListData.forEach(item => item.isChecked = false)
				obj.isChecked = true
				// console.log('选择了', event, ID)
				// //如果购物车里的商品id = 点击数据商品id 则返回此元素下标
				// let newIndex = this.selectInvetTypeListData.findIndex(ev => ev.Id === ID)
				// //如果购物车里的商品id = 点击数据商品id 则返回此元素下标
				// let value = event.detail.value
				// console.log('返回此元素的下标', value, ID, index, newIndex)
				// if (newIndex == -1) {
				// 	if (value.length == 0 && this.inventypeListData[index].Id === ID) {

				// 	} else {
				// 		this.selectInvetTypeListData.push(this.inventypeListData[index])
				// 	}
				// } else {
				// 	// console.log('已经存在',this.selectInvetTypeListData,)
				// 	this.selectInvetTypeListData.splice(newIndex, 1)
				// }
				// console.log('获取选中好的数据', this.selectInvetTypeListData)

			},
			// 选好了
			submitButton() {
				// console.log('最终提交的数组', this.selectInvetTypeListData)
				// let arr = []
				// this.selectInvetTypeListData.forEach(item => {
				// 	let obj = {
				// 		CompanyId: this.CommpayInfo.Id,
				// 		industryId: item.Id
				// 	}
				// 	arr.push(obj)
				// })
				let arr = this.inventypeListData.filter(item => item.isChecked).map(item => {
					return {
						CompanyId: this.CommpayInfo.Id,
						industryId: item.Id
					}
				})
				if (arr.length <= 0) return this.$api.msg('请选择行业')
				uni.showLoading({
					mask: true

				})
				this.$http("post", "/BO/Industry_Company/AddIndustryCompany", arr)
					.then((res) => {
						uni.hideLoading()
						console.log(res);
						if (res.Success) {
							this.$api.msg('提交成功')
							// 返回上一页
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							this.$api.msg(res.Msg)
						}
					});

			}
		}
	}
</script>

<style scoped lang="less">
	.container {
		.scroll {
			padding-top: 30rpx;
			width: 99%;
			// 兼容H5和APP
			/* #ifdef H5 */
			height: calc(100vh - 120px);
			/* #endif */
			/* #ifdef APP */
			height: calc(100vh - 120px);

			/* #endif */
			.scrollList {
				background-color: #fff;

				.scrollItem {
					padding: 10rpx 20rpx;
					margin-bottom: 10rpx;
					display: flex;
					align-items: center;
				}
			}

		}

		.submitButton {
			position: fixed;
			/* bottom: 100rpx; */
			bottom: 0;
			width: 100%;
			height: 60rpx;
			/*暂时发现安卓小屏手机安全区为0 && 不带单位的话padding-bottom会失效，写一个兜底, 并写在最上面；f12发现css样式如果都给height设置样式，会依次往下，用最底下的一个样式，如果最下面的没失效，依次往上查找，所以兜底的写在最上面 */
			height: calc(80rpx + constant(safe-area-inset-bottom));
			/* 直接扩展高度，因为padding-bottom是内边距 */
			height: calc(115rpx + env(safe-area-inset-bottom));
			padding-top: 15rpx;
			/* 直接扩展高度 */
			box-sizing: border-box;
			padding-bottom: constant(safe-area-inset-bottom);
			/*兼容 iOS<11.2 */
			padding-bottom: env(safe-area-inset-bottom);
			border-top: 1px solid #DBDBDB;
			/* 兼容iOS>= 11.2*/
			left: 0;

			.button {
				margin: 0 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #0BA8F0;
				border-radius: 10rpx;
				color: #fff;
				padding: 15rpx;
				font-weight: bold;
			}

		}
	}
</style>